package com.gxa.security.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.common.base.RespResult;
import com.gxa.security.dto.AddUserDTO;
import com.gxa.security.dto.FindUserDTO;
import com.gxa.security.dto.ResetPassword;
import com.gxa.security.entity.User;
import com.gxa.security.mapper.UserMapper;
import com.gxa.security.security.security.DefaultPasswordEncoder;
import com.gxa.security.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 25688
 * @description 针对表【user】的数据库操作Service实现
 * @createDate 2023-04-08 09:40:53
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {


    @Resource
    DefaultPasswordEncoder defaultPasswordEncoder;
    @Resource
    UserMapper userMapper;

    @Override
    public RespResult findAllUser(FindUserDTO findUserDTO) {

        Page<User> page = new Page<>(findUserDTO.getPageNum(), findUserDTO.getPageSize());
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//        根据账号状态查询
        if (findUserDTO.getStatus() != null && !"".equals(findUserDTO.getStatus())) {
            queryWrapper.eq("status", findUserDTO.getStatus());
        }
//根据角色查询
        if (findUserDTO.getRoleId() != null && !"".equals(findUserDTO.getRoleId())) {
            queryWrapper.eq("role_id", findUserDTO.getRoleId());
        }
//        根据用户名称查询
        queryWrapper.eq(StringUtils.isNotBlank(findUserDTO.getRealName()), "real_name", findUserDTO.getRoleId());

        Page<User> page1 = userMapper.selectPage(page, queryWrapper);


        if (page1.getRecords().size() > 0) {
            return RespResult.success(page1.getRecords());
        }
        return RespResult.error("当前数据无此数据");
    }


    @Override
    public RespResult findByUserId(long userId) {
        User user = userMapper.selectById(userId);
        if (user != null) {
            return RespResult.success(user);
        }
        return RespResult.error("后台出现错误，请再次尝试");
    }

    @Override
    public RespResult updateUser(User user) {

        int i = userMapper.updateById(user);
        if (i > 0) {
            return RespResult.success();
        }
        return RespResult.error("后台出现错误，请再次尝试");
    }

    @Override
    public RespResult resetPassword(ResetPassword resetPassword) {

        if (resetPassword.equals(resetPassword.getConfirmPassword())) {
            return RespResult.error("两次密码不一致，请重新输入");
        }
        String encode = defaultPasswordEncoder.encode(resetPassword.getConfirmPassword());

        int i = userMapper.resetPassword(resetPassword.getUserId(), encode);
        if (i > 0) {
            return RespResult.success("修改密码成功");
        }
        return RespResult.error("后台出现错误，请再次尝试");
    }

    @Override
    public RespResult updateStatus(long userId) {
        int i = userMapper.updateStatus(userId);
        if (i > 0) {
            return RespResult.success();
        }
        return RespResult.error("后台出现错误，请再次尝试");
    }

    @Override
    public RespResult addUser(AddUserDTO userDTO) {

        User user = BeanUtil.copyProperties(userDTO, User.class);
        user.setUsername(userDTO.getEmail());
        user.setPassword("000000");
        int insert = userMapper.insert(user);
        if (insert > 0) {
            return RespResult.success();
        }


        return RespResult.error("添加失败，请再次尝试！");
    }
}




